// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import CM from "../../../lib/cache/CM";
import ICache from "../../../lib/cache/ICache";
import { monitor, selfCls } from "../../../lib/common/Decorator";
import GameObject from "../../../lib/object/GameObject";
import DField from "../../cache/DField";
import DMachine, { MachineCacheArgs } from "../../cache/DMachine";
import DBMachine from "../../config/DBMachine";
import DBMap from "../../config/DBMap";
import Render, { RenderArgs } from "../ability/Render";

export interface MachineArgs {
    row:number;
    col:number;
    id:number;
}

const {ccclass, property} = cc._decorator;

@selfCls
export default class GOMachine extends GameObject {
    public async init(args?: MachineArgs): Promise<void> {
        this.cache<DMachine>().row = args.row;
        this.cache<DMachine>().col = args.col;
        this.cache<DMachine>().machine = args.id;
        super.init(args);
        
    }
    initCache(): ICache<MachineCacheArgs> {
        return CM.ins(CM).create(DMachine, {id:this.uuid});
    }
    async initSection() {
        let path = DBMachine.getRes(this.cache<DMachine>().machine);
        await this.addCCMP<Render, RenderArgs>(Render, {path:path});
        
    }


    @monitor()
    on_row_change(attrName, oldVal, newVal) {
        let y = DBMap.getMapSize()/2+this.cache<DMachine>().row*DBMap.getMapSize();
        y-=CM.ins(CM).get(DField).height/2;
        this.node.y = y;
    }

    @monitor()
    on_col_change(attrName, oldVal, newVal) {
        let x = DBMap.getMapSize()/2+this.cache<DMachine>().col*DBMap.getMapSize();
        x-=CM.ins(CM).get(DField).width/2;
        this.node.x = x;
    }

    
}
